package cn.rbac.dao.imp;

import java.util.List;

import org.hibernate.Session;

import cn.rbac.dao.IResourceDao;
import cn.rbac.model.Resource;
import cn.rbac.util.HibernateUtil;

public class ResourceDaoImp extends BaseDaoImp<Resource> implements IResourceDao{

	public Resource getMenu(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Resource menu =(Resource) session.createQuery( "from Resource where parentIds=0" ).uniqueResult();//0级菜单
		
		List<Resource> secMenu=getChildMenu(menu.getIds(), session);
		if(secMenu!=null){
			menu.setChild(secMenu);//1级菜单
			for (Resource resource2 : secMenu) {
				List<Resource> tdMenu=getChildMenu(resource2.getIds(), session);
				if (null!=tdMenu) {
					resource2.setChild(tdMenu);//2级菜单
				}
			}
		}

		session.getTransaction().commit();
		session.close();
		return menu;
	}
	
	private List<Resource> getChildMenu(int id,Session session){
		List<Resource> child=session.createQuery(
				"from Resource where parentIds=:pid").//
				setParameter("pid", id).list();//
		return child;
	}

}
